package ma.ensao.evalmetrics.view;

import java.util.List;

import ma.ensao.evalmetrics.controller.UserManager;
import ma.ensao.evalmetrics.model.User;

import com.opensymphony.xwork2.ActionSupport;

/*
* Actions sur les Users (Ajout, Modifications, Recherche)
*/
 
public class UserValidAction extends ActionSupport {

	private static final long serialVersionUID = 9149826260758390091L;
	private User user;
	private List<User> userList;
	private Long id;
    private UserManager linkController;

	public UserValidAction() {
		linkController = new UserManager();
	}
	public String execute() {
		System.out.println("exec");
		this.userList = linkController.list();
		return SUCCESS;
	}
	public String add() {
		System.out.println("UserAction add "+user.getNom()+"\n");
		if (exists() == true){
			try {
				linkController.add(getUser());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return SUCCESS;
		}else{
			return "input";
		}
		
	}
	public String modifier() {
		System.out.println("UserAction add "+user.getId()+"\n");
		try {
			linkController.modifier(getUser());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public boolean exists() {
		System.out.println("adminAction +++++++++++++++++"+ user.getUserLogin()+"\n");
		boolean statut;
		statut= linkController.userExists(user.getUserLogin());
		if(statut == true){//login existe
            addActionError(getText("error.loginExiste"));
            return false;
		}else{//login n'existe pas
			return true;
		}
	 }
	public User getUser() {
		return user;
	}
	public List<User> getUserList() {
		return userList;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setuserList(List<User> userList) {
		this.userList = userList;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
}
